using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter24
{
	/// <summary>
	/// Example01: Electric Flux Through a Sphere
	/// What is the electric flux through a sphere that has a radius
	/// of 1.0 m and carries a charge of 1.0 \mu C at its center?
	/// \Phi_E = 1.13 \times 10^5 Nm^2/C
	/// </summary>
	public class Example01
	{
		public Example01()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		public void Compute()
		{
			L.Vector.Field vf = new L.Vector.Field(efield);
			L.ElectricField E = new L.ElectricField();
            E.VectorField = vf;

			L.Surface.Parameterization paraToPosition
				= new L.Surface.Parameterization(func);
			L.Surface S = new L.Surface();
            S.ParameterToPosition = paraToPosition;
			S.Parameter1StartValue = 0.0;
			S.Parameter1EndValue = Math.PI;
			S.Parameter2StartValue = 0.0;
			S.Parameter2EndValue = 2.0*Math.PI;

			L.Time t = new L.Time();
			L.ElectricFlux Phi = new L.ElectricFlux(E,S,t);
			result+=Convert.ToString(Phi.NmSQUAREPERC)+"  +/-   "+
				Convert.ToString(Phi.StandardDeviation);
		}
		private L.Vector efield(L.Position r, L.Time t)
		{
			L.Vector v = new L.Vector();
			double n = Math.Sqrt(r.X*r.X+r.Y*r.Y+r.Z*r.Z);
			double c = 1.0/4.0/Math.PI/L.Constant.PermittivityOfFreeSpace;
			double q = 1.0E-6;
			v.X = c*q/n/n*r.X/n;
			v.Y = c*q/n/n*r.Y/n;
			v.Z = c*q/n/n*r.Z/n;
			return v;
		}
		private L.Position func(double x, double y)
		{
			L.Position r = new L.Position();
            r.X = 1.0*Math.Sin(x)*Math.Cos(y);
            r.Y = 1.0*Math.Sin(x)*Math.Sin(y);
            r.Z = 1.0*Math.Cos(x);
			return r;
		}
	}
}
